package linkedlist.josephus;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : Rookie
 * @description : 约瑟夫环 循环解法
 * @date : 2023/6/6
 */
public class CyclicMethod {

    public static void main(String[] args) {
        josephus(20,5);
    }

    private static void josephus(int n,int m) {
        // 所有人入队
        List<Integer> total = new ArrayList<>(n);
        for (int i = 1; i <= n; i++) {
            total.add(i);
        }

        // 记录叫号
        int count = 0;
        // 记录索引
        int index = 0;
        StringBuilder sb = new StringBuilder();
        while (total.size() > 1) {
            count ++;
            if (count == m) {
                sb.append(total.get(index)).append(" ");
                total.remove(index);
                count = 0;
            } else {
                index++;
            }
            if (index == total.size()) {
                index = 0;
            }
        }
        System.out.println("出列顺序为： " + sb.append(total.get(0)));
    }
}
